<?php

class Helper_String
{

    static function getUnitByQty($qty, $unit){

        $unit = strtoupper($unit);
        if($qty < 1) return $unit;
        if(!QValidator::validate_is_alpha($unit)) return $unit;

        $specialPlural = array(
            'M' => 'M',
        );
        if(isset($specialPlural[$unit])) return $specialPlural[$unit];

        return $unit.'S';
    }

    static function limit($str, $limit = 50, $suffix = '...')
    {

        $tmpLimit = $limit - strlen($suffix);

        if (strlen($str) > $tmpLimit) {
            $str = substr($str, 0, $tmpLimit) . $suffix;
        }

        return $str;
    }

    static function convertPunctuation($str)
    {
        $map = array(
            '～' => '~',
            '！' => '!',
            '＠' => '@',
            '＃' => '#',
            '％' => '%',
            '＆' => '&',
            '（' => '(',
            '）' => ')',
            '－' => '-',
            '＝' => '=',
            '＋' => '+',
            '｛' => '{',
            '｝' => '}',
            '【' => '[',
            '】' => ']',
            '＼' => '\\',
            '｜' => '|',
            '；' => ';',
            '：' => ':',
            '‘' => "'",
            '“' => '"',
            '《' => '<',
            '》' => '>',
            '，' => ',',
            '。' => '.',
            '、' => ',',
            '？' => '?',
            '’' => '/',
        );

        $search = array_keys($map);
        $replace = array_values($map);
        return str_replace($search, $replace, $str);
    }

    static function normalizeNumber($val)
    {
        if (is_string($val)) {
            $search = array(',', ' ');
            return str_replace($search, '', $val);
        }

        return $val;
    }

    static function shortMonthFormatToLong($mon)
    {
        $map = array(
            'jan' => 'January',
            'feb' => 'February',
            'mar' => 'March',
            'apr' => 'April',
            'may' => 'May',
            'jun' => 'June',
            'jul' => 'July',
            'aug' => 'August',
            'sep' => 'September',
            'oct' => 'October',
            'nov' => 'November',
            'dec' => 'December',
        );

        $mon = strtolower($mon);
        if (isset($map[$mon])) return $map[$mon];

        throw new Exception('invalid short month format');
    }

    static function spellNumber($val)
    {
        $ret = '';
        $val = number_format(self::normalizeNumber($val), 2);
        list($int, $decimal) = explode('.', $val);
        $intParts = explode(',', $int);

        $thousands = array('', 'THOUSAND', 'MILLION', 'BILLION', 'TRILLION');
        $thousands = array_reverse(array_slice($thousands, 0, count($intParts)));

        foreach ($intParts as $i => $part) {
            if ($i !== 0) $useAnd = true;
            if ('000' !== $part) $ret .= ' ' . self::spellNumberPart($part, $useAnd) . ' ' . $thousands[$i];
        }

        if ($decimal !== '00') {
            $ret .= ' AND CENTS ' . self::spellNumberPart($decimal);
        }

        return trim(preg_replace('/\s{1,}/', ' ', $ret));
    }

    protected static function spellNumberPart($val, $useAnd = false)
    {

        $tens = array(0 => '', 1 => '', 2 => 'TWENTY', 3 => 'THIRTY', 4 => 'FORTY', 5 => 'FIFTY', 6 => 'SIXTY', 7 => 'SEVENTY', 8 => 'EIGHTY', 9 => 'NINETY');
        $ones = array(0 => '', 1 => 'ONE', 2 => 'TWO', 3 => 'THREE', 4 => 'FOUR', 5 => 'FIVE', 6 => 'SIX', 7 => 'SEVEN', 8 => 'EIGHT', 9 => 'NINE', 10 => 'TEN', 11 => 'ELEVEN', 12 => 'TWELVE', 13 => 'THIRTEEN', 14 => 'FOURTEEN', 15 => 'FIFTEEN', 16 => 'SIXTEEN', 17 => 'SEVENTEEEN', 18 => 'EIGHTEEN', 19 => 'NINETEEN');

        $ret = '';
        $val = ltrim($val, '0');
        $and = '';
        if ($useAnd) $and = ' &';
        if (!$val) $and = '';

        // if $val has 3 digit
        if ($val > 99) {
            $ret .= ' ' . $ones[$val[0]] . ' HUNDRED';
            $val = substr($val, 1);
            $val = ltrim($val, '0');
            if ($val) {
                $and = ' &';
            } else {
                $and = '';
            }
        }

        // now $val has two digit and great than 19
        if ($val > 19) {
            $ret .= $and . ' ' . $tens[$val[0]];
            $val = substr($val, 1);

            // and is used, clear it
            $and = '';
        }

        // now $val should be 0 ~ 19
        $ret .= $and . ' ' . $ones[$val];

        return $ret;
    }
}

?>